<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Goods as GoodsModel;
use app\common\model\Order as OrderModel;
use app\common\model\GoodsOption as GoodsOptionModel;
use app\common\model\GoodsIlike as GoodsIlikeModel;
use app\common\model\GoodsGroup as GoodsGroupModel;
use think\Db;
/**
 * 禅旅控制器
 * Class Goods
 * @package app\home\controller
 */
class Goods extends Base
{
    /**
     * [getIndex  列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new GoodsModel;
        $gpmodel = new GoodsOptionModel();
        $map = [];
        $this->getPageAndSize($param);
        try{
            $map['status'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map,$this->from, $this->size,'id,title,cover');
            foreach($lists as &$v){
                $option = $gpmodel->getMin(['goodsid'=>$v['id'],'status'=>1]);
                $v['price'] = number_format($option,2);
                $v['cover'] = explode(',',$v['cover']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getContent  获取内容]
     * @param
     * @return json
     * @author [C。]
     */
    public function getContent()
    {
        $param = input('param.');
        $model = new GoodsModel;
        $gmodel = new GoodsGroupModel;
        $gimodel = new GoodsIlikeModel;
        $omodel = new OrderModel;
        $gpmodel = new GoodsOptionModel();
        $memid = $this->user;
        $map = [];
        $map1 = [];
        $map2 = [];
        try{
            $good = $model->findData($param['id'],'id','*');
            $good['cover'] = explode(',',$good['cover']);
            $map1['status'] = 1;
            $map1['goodsid'] = $param['id'];
            $count = $omodel->getAllCount($map1);
            $good['month_num'] = $count;
            $ilike = $gimodel->findOne(['goodsid'=>$param['id'],'memid'=>$memid['id'],'status'=>1]);
            if(!empty($ilike)){
                $good['ilike'] = 1;
            }else{
                $good['ilike'] = 2;
            }

            $map['o.goodsid'] = $param['id'];
            $map['g.addtime'] = ['between',[time() - 24 * 3600 * config('home.cancel_time'),time() + 24 * 3600 * config('home.cancel_time')]];
            $map['o.status'] = 1;
            $map['go.person_num'] = ['exp',Db::raw('>g.num')];
            $map['o.refundid'] = ['eq',0];
            $groupnum = $gmodel->getCount($map);
            $group = $gmodel->getList($map,0,2,'g.id,o.optionid oid,m.img,m.username,go.person_num,go.day,o.gotime,g.num,g.addtime');
            foreach($group as &$v){
                $v['residue'] = 24*3600 * config('home.cancel_time') - (time() - $v['addtime']);
                $v['gotime'] = date('Y-m-d',$v['gotime']);
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $map2['status'] = 0;
                $map2['create_time'] = ['gt',time()-15*60];
                $num = $omodel->getSum($map2,'num');
                if($v['person_num'] <= $num + $v['num']){
                    $v['isshow'] = 0;
                }else{
                    $v['isshow'] = 1;
                }
            }
            $good['group'] = $group;
            $minprice = $gpmodel->getMin(['goodsid'=>$param['id'],'status'=>1]);
            $good['minprice'] = number_format($minprice,2);
            $good['groupnum'] = $groupnum;
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$good);
    }

    /**
     * [getIndex  列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getHome()
    {
        $model = new GoodsModel();
        $gpmodel = new GoodsOptionModel();
        $map = [];
        try{
            $goods = $model->getList($map,0,2,'id,title,cover');
            foreach($goods as &$v){
                $option = $gpmodel->getMin(['goodsid'=>$v['id'],'status'=>1]);
                $v['price'] = number_format($option,2);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$goods);
    }
}